package cn.qf.shop.controller;
import cn.qf.shop.pojo.entity.Activities;
import cn.qf.shop.pojo.qo.ActivitiesQO;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.ActivitiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Resource;
import java.util.List;

@EnableSwagger2
@RequestMapping("/act")
@Api(tags = "活动管理业务接口")
@RestController
@RestControllerAdvice
public class  ActivitiesController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ActivitiesController.class);
    @Resource
    ActivitiesService activitiesService;

    /**
     * 查询所有人员信息列表，返回给前端页面
     *
     * @return
     */
    @ResponseBody
    @ApiOperation("活动管理数据展示")
    @PostMapping(value = "/showAct", produces = {"application/json;charset=UTF-8"})
    public RespResult showAct() {
        List<Activities> activities = activitiesService.showAct();
        RespResult respResult = null;
        if (activities == null) {
            respResult = super.returnRespWithFailed("找不到用户列表");
        }
        respResult = super.returnRespWithSuccess(activities);
        return respResult;
    }

    /**
     * 增加一条字段
     *
     * @param activitiesQO
     * @return
     */
    @ResponseBody
    @ApiOperation("增加数据")
    @PostMapping(value = "/regAct", produces = {"application/json;charset=UTF-8"})
    public RespResult regAct(@RequestBody ActivitiesQO activitiesQO) {
        Activities activities = new Activities();
        BeanUtils.copyProperties(activitiesQO, activities);
        RespResult respResult = new RespResult();
        int i = activitiesService.addAct(activitiesQO);
        if (i == 1) {
            respResult = showAct();
        } else {
            respResult = returnRespWithFailed("添加失败");
        }
        return respResult;
    }

    /**
     * 点击编辑
     *
     * @param activitiesQO
     * @return
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/alterAct", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult alterAct(@RequestBody ActivitiesQO activitiesQO) {
        Activities activities = new Activities();
        BeanUtils.copyProperties(activitiesQO, activities);
        RespResult respResult = new RespResult();
        int i = activitiesService.updateAct(activitiesQO);
        if (i == 1) {
            respResult = showAct();
        } else {
            respResult = returnRespWithFailed("修改失败");
        }
        return respResult;
    }

    /**
     * 点击删除一条记录
     *
     * @param actId
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteActId", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteActId(int actId) {

        RespResult respResult = new RespResult();
        int deleteAct = activitiesService.deleteAct(actId);
        if (deleteAct == 1) {
            respResult = showAct();
        } else {
            respResult = returnRespWithFailed("删除失败");
        }
        return respResult;
    }

    /**
     * 点击图标进行全部删除
     *
     * @param actIds
     * @return
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteAllAct", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteAllAct(@RequestParam(value = "actIds[]") List<Integer> actIds) {
        int i = activitiesService.deleteAllAct(actIds);
        RespResult respResult = null;
        if (i == -1) {
            respResult = super.returnRespWithFailed("删除失败");
        } else {
            respResult = showAct();
        }
        return respResult;
    }

    @ResponseBody
    @ApiOperation("用户模糊查询")
    @RequestMapping(value = "/selectAllAct", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult selectAllAct(@RequestParam(required = false) String keyword, @RequestParam(required = false) String createTime, @RequestParam(required = false) String updateTime, @RequestParam(defaultValue = "1") int limit, @RequestParam(defaultValue = "10") int offset) {
        RespResult respResult = null;
        List<ActivitiesQO> activitiesQOS = activitiesService.ActivitiesList(keyword, createTime, updateTime, limit, offset);
        if (activitiesQOS.size() == 0) {
            respResult = returnRespWithFailed("找不到实体类");
        } else {
            RespResult respResult1 = returnRespWithSuccess(activitiesQOS);
            return respResult1;
        }
        return respResult;
    }
}